package com.example.persistence;

import android.content.Intent;

import com.example.R;
import com.example.entity.SelectMode;
import com.example.module.switcher.ActivitySwitcher;

public abstract class SwitchActivity extends ImageActivity {
	private boolean isSwitched = false;;
	protected boolean enableSwitchAnimation = true;

	@Override
	protected void onResume() {
		if (enableSwitchAnimation) {
			if (isSwitched)
				ActivitySwitcher.animationRightIn(findViewById(R.id.container), getWindowManager());
			else
				ActivitySwitcher.animationLeftIn(findViewById(R.id.container), getWindowManager());	
		}
		
		super.onResume();
	}
 
	protected void switchActivity(final Class<?> clazz) {
		isSwitched = true;
		ActivitySwitcher.animationLeftOut(findViewById(R.id.container), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
			@Override
			public void onAnimationFinished() {
				Intent intent = new Intent(getApplicationContext(), clazz);
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(intent);
			}
		});
	}
	
	protected void switchActivity(final Intent intent) {
		isSwitched = true;
		ActivitySwitcher.animationLeftOut(findViewById(R.id.container), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
			@Override
			public void onAnimationFinished() {
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(intent);
			}
		});
	}
	
	protected void switchActivityForResult(final int requestCode, final Class<?> clazz) {
		isSwitched = true;
		ActivitySwitcher.animationLeftOut(findViewById(R.id.container), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
			@Override
			public void onAnimationFinished() {
				Intent intent = new Intent(getApplicationContext(), clazz);
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(intent, requestCode);
			}
		});
	}
	
	protected void switchActivityForResult(final Intent intent, final int requestCode) {
		isSwitched = true;
		ActivitySwitcher.animationLeftOut(findViewById(R.id.container), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
			@Override
			public void onAnimationFinished() {
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(intent, requestCode);
			}
		});
	}
	
	protected void finishActivity() {
		if (enableSwitchAnimation) {
			isSwitched = false;
			ActivitySwitcher.animationRightOut(findViewById(R.id.container), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
				@Override
				public void onAnimationFinished() {
					finish();
				}
			});	
		}
		else
			finish();
		
	}
	
	protected void finishActivityForResult(final int resultCode, final Intent data) {
		isSwitched = false;
		ActivitySwitcher.animationRightOut(findViewById(R.id.container), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
			@Override
			public void onAnimationFinished() {
				setResult(resultCode, data);
				finish();
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		if (selectMode != SelectMode.NONE)
			super.onBackPressed();
		else
			finishActivity();
	}
}
